//MIT License
//Copyright (c) 2024 nuaazgg, Nanjing University of Aeronautics and Astronautics(NUAA)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.IO;
using TMPro;
// using UnityEngine.SceneManagement;

public class AssemblyProcessing : MonoBehaviour
{
    int count = 0;
    TMP_Text text;
    Animation PartAnimation;
    string content, PartNamecontent;
    public GameObject FinishUI;

    public void ProcessCount()
    {
        count++;
        if(count < 30)
        {
            TextChange(count);
            PartChangeActive(count);
            if ((count - 1) >= 0)
            {
                PartChangeStop(count - 1);
            }
        }
        else if(count == 30)
        {
            PartChangeStop(count - 1);
            GameObject InstallUI = GameObject.Find("AssemblyProcessing/InstallationProcess");
            InstallUI.SetActive(false);
            FinishUI.SetActive(true);
        }

    }

    public void PreviousCount()
    {
        count--;
        if (count <= 30)
        {
            TextChange(count);
            if((count - 1) >= -1)
            {
                PartChangeFalse(count + 1);
                PartChangePlay(count);
            }
                        
        }

    }

    private void PartChangeActive(int i)
    {
        string[] ENPartName = File.ReadAllLines(Application.streamingAssetsPath + "/ENPartName.txt");
        PartNamecontent = ENPartName[i];
        GameObject ParentObj = GameObject.Find("AssemblyProcessing/MonaAssembly/ModelGuided");
        GameObject Child = ParentObj.transform.Find(PartNamecontent).gameObject;
        Child.SetActive(true);
    }

    private void PartChangeFalse(int i)
    {
        string[] ENPartName = File.ReadAllLines(Application.streamingAssetsPath + "/ENPartName.txt");
        PartNamecontent = ENPartName[i];
        GameObject Child = GameObject.Find(PartNamecontent);
        Child.SetActive(false);
    }

    private void PartChangePlay(int i)
    {
        string[] ENPartName = File.ReadAllLines(Application.streamingAssetsPath + "/ENPartName.txt");
        PartNamecontent = ENPartName[i];
        GameObject Child = GameObject.Find(PartNamecontent);
        PartAnimation = Child.GetComponent<Animation>();
        PartAnimation.Play();
    }

    private void PartChangeStop(int i)
    {
        string[] ENPartName = File.ReadAllLines(Application.streamingAssetsPath + "/ENPartName.txt");
        PartNamecontent = ENPartName[i];
        GameObject ParentObj = GameObject.Find("AssemblyProcessing/MonaAssembly/ModelGuided");
        GameObject Child = ParentObj.transform.Find(PartNamecontent).gameObject;
        PartAnimation = Child.GetComponent<Animation>(); ;
        PartAnimation.Stop();
        Child.transform.localPosition = new Vector3(0, 0, 0);
    }

    private void TextChange(int i)
    {
        string[] PartName = File.ReadAllLines(Application.streamingAssetsPath + "/PartName.txt");
        text = GameObject.Find("AssemblyProcessing/InstallationProcess/PartName/PartNameText").GetComponent<TMP_Text>();
        content = PartName[i];
        text.text = content;
    }


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
